package com.joven.shelltest.test;

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 延时定时循环任务
 *
 * @Author wzj
 * @Date 2023-09-20 10:46
 **/
public class OtherTest9 {
    public static void main(String[] args) {
//        initial：表示第一次开始任务时要延迟的时间
//        period：表示每次执行此任务要间隔的时间
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
        System.out.println("X  " + LocalDateTime.now());
        executor.scheduleAtFixedRate(() -> {
            System.out.println("begin A " + LocalDateTime.now());
            System.out.println("this thread:" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end A " + LocalDateTime.now());
        }, 2, 4, TimeUnit.SECONDS);
        System.out.println("Y  " + LocalDateTime.now());
    }
}
